from flask import Blueprint
from flask_login import login_required

from apps.a_common.db import safe_commit
from apps.a_common.log import MyLogger
from apps.a_common.permission import has_permission_decorate
from apps.a_common.request import assert_params_by_form
from apps.a_common.response import paginate_response, success_response
from apps.foundation import db
from apps.projects.forms import ProcessSearchForm, ProcessEditorForm
from apps.projects.models import Process
from apps.projects.serializers import serialize_process

process_blueprint = Blueprint('process', __name__)
log = MyLogger('process views')


@process_blueprint.route('/processes/<int:process_id>/', methods=['GET'])
@login_required
@has_permission_decorate('admin-process-view')
def _get_process(process_id):
    """
    查看一个环节的详情
    ---
    tags:
      - process
    parameters:
      - name: process_id
        in : path
        type: integer
        required: true
        description: 项目的id
    """
    return success_response(serialize_process(Process.query.filter(Process.id == process_id).first_or_404()))


@process_blueprint.route('/processes/', methods=['GET'])
@login_required
@has_permission_decorate('admin-process-view')
def _get_processes():
    """
    获取环节列表
    ---
    tags:
      - process
    parameters:
      - name: project_id
        in : query
        type: integer
        required: false
        description: 项目id找环节
      - name: status
        in : query
        type: integer
        required: false
        description: 查找状态
      - name: page_id
        in : query
        type: integer
        required: false
        default: 1
        description: 页码
      - name: page_size
        in : query
        type: integer
        required: false
        default: 20
        description: 每页的大小
    """
    form = assert_params_by_form(ProcessSearchForm)
    queryset = Process.query
    if form.get('project_id'):
        queryset = queryset.filter(Process.project_id == form['project_id'])
    if form.get('status'):
        queryset = queryset.filter(Process.status == form['status'])
    return paginate_response(queryset, serialize_process)


@process_blueprint.route('/processes/', methods=['POST'])
@login_required
@has_permission_decorate('admin-process-create')
def _post_processes():
    """
    新建环节
    ---
    tags:
      - process
    parameters:
      - name: name
        in : formData
        type: string
        required: true
        description: 环节的名称
      - name: start_at
        in : formData
        type: integer
        required: false
        description: 环节的开始时间
      - name: end_at
        in : formData
        type: integer
        required: false
        description: 环节的结束时间
      - name: project_id
        in : formData
        type: integer
        required: true
        description: 项目的id
    """
    form = assert_params_by_form(ProcessEditorForm)
    process = Process()
    for key, value in form.items():
        setattr(process, key, value)
    db.session.add(process)
    safe_commit()
    return success_response(serialize_process(process))


@process_blueprint.route('/processes/<int:process_id>/', methods=['PUT'])
@login_required
@has_permission_decorate('admin-process-change')
def _put_process(process_id):
    """
    修改环节信息
    ---
    tags:
      - process
    parameters:
      - name: process_id
        in : path
        type: integer
        required: true
        description: 环节的id
      - name: name
        in : formData
        type: string
        required: false
        description: 环节的名称
      - name: start_at
        in : formData
        type: integer
        required: false
        description: 环节的开始时间
      - name: end_at
        in : formData
        type: integer
        required: false
        description: 环节的结束时间
      - name: status
        in : formData
        type: integer
        required: false
        description: 环节的状态
    """
    form = assert_params_by_form(ProcessEditorForm)
    process = Process.query.filter(Process.id == process_id).first_or_404()

    for key, value in form.items():
        setattr(process, key, value)
    safe_commit()
    return success_response(serialize_process(process))
